/*
 * @Author: nqx2007 fenglimima334@163.com
 * @Date: 2023-12-04 18:00:36
 * @LastEditors: nqx2007 fenglimima334@163.com
 * @LastEditTime: 2023-12-04 21:40:52
 * @FilePath: /operator-manager/src/app.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 运行时配置
import {ProBreadcrumb} from '@ant-design/pro-components'
import {getUserInfo} from '@/services/user';
import { history } from '@umijs/max';
import { errorConfig } from '../config/requestErrorConfig';
import type { Settings as LayoutSettings } from '@ant-design/pro-components';

import { loginPath } from './constants';

export async function getInitialState(): Promise<{
  settings?: Partial<LayoutSettings>;
  currentUser?: Record<string,any>;
  loading?: boolean;
  fetchUserInfo?: () => Promise<Record<string,any> | undefined>;
}> {
  const fetchUserInfo = async () => {
    try {
      const response = await getUserInfo({
        skipErrorHandler: true,
      });
      return {
        ...response.user,
        permissions: response.permissions,
        roles: response.roles,
      } as Record<string,any>;
    } catch (error) {
      console.log(error);
      history.push(loginPath);
    }
    return undefined;
  };
  // 如果不是登录页面，执行
  const { location } = history;
  if (location.pathname !== loginPath) {
    const currentUser = await fetchUserInfo();
    return {
      fetchUserInfo,
      currentUser,
    };
  }
  return {
    fetchUserInfo
  };
}



export const layout = () => {
  return {
    layout:'mix',
    logo:"https://img.alicdn.com/tfs/TB1YHEpwUT1gK0jSZFhXXaAtVXa-28-27.svg",
    title:'万惠券小二后台',
    menu: {
      locale: false,
    },
    headerTitleRender:(logo)=>{
      return (
        <div style={{display:'flex',alignContent:'flex-start',alignItems:'center',width:"256px"}}>
          <span>{logo}</span>
          <span style={{lineHeight:'18px',fontSize:'14px',paddingLeft:'10px'}}>万惠券<br />小二后台</span>
        </div>
      )
    },
    headerContentRender:() => {
      return <ProBreadcrumb />;
    },
    rightContentRender:()=><>用户名</>
  };
};


//请求配置
export const request = {
  ...errorConfig,
  // requestInterceptors: [
  //   (url: any, options: { headers: any }) => {
  //     const headers = options.headers ? options.headers : [];
  //     console.log('request ====>:', url);
  //     const authHeader = headers['Authorization'];
  //     const isToken = headers['isToken'];
  //     if (!authHeader && isToken !== false) {
  //       const expireTime = getTokenExpireTime();
  //       if (expireTime) {
  //         const left = Number(expireTime) - new Date().getTime();
  //         const refreshToken = getRefreshToken();
  //         if (left < checkRegion && refreshToken) {
  //           if (left < 0) {
  //             clearSessionToken();
  //           }
  //         } else {
  //           const accessToken = getAccessToken();
  //           if (accessToken) {
  //             headers['Authorization'] = `Bearer ${accessToken}`;
  //           }
  //         }
  //       } else {
  //         clearSessionToken();
  //       }
  //     }
  //     return { url, options };
  //   },
  // ],
  // responseInterceptors: [
    // (response) =>
    // {
    //   // // 不再需要异步处理读取返回体内容，可直接在data中读出，部分字段可在 config 中找到
    //   // const { data = {} as any, config } = response;
    //   // // do something
    //   // console.log('data: ', data)
    //   // console.log('config: ', config)
    //   return response
    // },
  // ],
};